﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;

namespace CPPEI.PM.Service.Model
{
    public class PMDBContext : DbContext
    {
        public PMDBContext()
            : base("name=PM")
        {
            Database.SetInitializer<PMDBContext>(null);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<WorkItemCategory>().ToTable("PM_WorkItemCategory");
            modelBuilder.Entity<WorkItemCategory>().HasKey(p => p.Id);

            modelBuilder.Entity<WorkItem>().ToTable("PM_WorkItem");
            modelBuilder.Entity<WorkItem>().HasKey(p => p.Id);

            modelBuilder.Entity<WorkItemCategoryRef>().ToTable("PM_WorkItemCategoryRef");
            modelBuilder.Entity<WorkItemCategoryRef>().HasKey(p => new { p.WorkItemCategoryId, p.WorkSheetId });

            modelBuilder.Entity<WorkItemRef>().ToTable("PM_WorkItemRef");
            modelBuilder.Entity<WorkItemRef>().HasKey(p => new { p.WorkItemId, p.WorkSheetId });

            modelBuilder.Entity<WorkStage>().ToTable("PM_WorkStage");
            modelBuilder.Entity<WorkStage>().HasKey(p => p.Id);

            modelBuilder.Entity<WorkStageRef>().ToTable("PM_WorkStageRef");
            modelBuilder.Entity<WorkStageRef>().HasKey(p => new { p.WorkStageId, p.WorkSheetId });

            modelBuilder.Entity<WorkStageItem>().ToTable("PM_WorkStageItem");
            modelBuilder.Entity<WorkStageItem>().HasKey(p => p.Id);

            modelBuilder.Entity<WorkStageItemRef>().ToTable("PM_WorkStageItemRef");
            modelBuilder.Entity<WorkStageItemRef>().HasKey(p => new { p.WorkStageItemId, p.WorkSheetId, p.WorkStageId });
            modelBuilder.Entity<WorkStageItemRef>().Property(p => p.WorkSheetId).HasColumnName("WorkSheetId");
            modelBuilder.Entity<WorkStageItemRef>().Property(p => p.WorkStageId).HasColumnName("WorkStageId");
            modelBuilder.Entity<WorkStageItemRef>().Property(p => p.WorkStageItemId).HasColumnName("WorkStageItemId");
            modelBuilder.Entity<WorkStageItemRef>().Property(p => p.ParentWorkStageItemId).HasColumnName("ParentWorkStageItemId");
            modelBuilder.Entity<WorkStageItemRef>().Property(p => p.Level).HasColumnName("Level");
        }
    }
}
